package cn.hpclub.server.controller.admin.goventerprise;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


import cn.hpclub.server.controller.admin.BaseAdminController;
import cn.hpclub.server.controller.admin.campus.BroadbandOpenOrderAdminController;
import cn.hpclub.server.model.CmccOrderSuspend;
import cn.hpclub.server.model.campus.BroadbandOpenOrder;
import cn.hpclub.server.service.campus.BroadbandOpenOrderService;
import cn.hpclub.server.service.goventerprice.CmccOrderSuspendService;
import cn.hpclub.server.util.ExcelRenderUtil;
import cn.hpclub.server.util.InterfaceUtil;
import cn.hpclub.server.util.QueryHelper;

public class SuspendReportController extends BaseAdminController<CmccOrderSuspend> {
    private static final String ORG_LIST = "orgList";
    private static final String ORG_ID   = "org_id";
    private static Logger logger = LoggerFactory.getLogger(SuspendReportController.class);

    int initOrgTree(){
        List<Map.Entry<String, String>> orgList = InterfaceUtil.getOrgList();
        if(orgList.size() > 0){
            /* 机构树可用 */
            setAttr(ORG_LIST, orgList);
            return Integer.parseInt(orgList.get(0).getValue());
        } else{
            return 0;
        }
    }
    public void list(){
        int defaultOrgId = initOrgTree();
        ConcurrentHashMap<String, String> searchInfo = getSearchInfo();
        if(searchInfo.get(ORG_ID) == null){
            searchInfo.put(ORG_ID, String.valueOf(defaultOrgId));
        }
        if(!searchInfo.containsKey("start_time") && !searchInfo.containsKey("end_time")){

            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            String endDate = sdf.format(new java.util.Date());
            Calendar c = Calendar.getInstance();
            try {
                c.setTime(sdf.parse(endDate));
            } catch (ParseException e) {
                e.printStackTrace();
            }
            c.add(Calendar.MONTH, -1); // 得到前一个月
            String startDate = sdf.format(c.getTime());
            searchInfo.put("start_time", startDate);
            searchInfo.put("end_time", endDate);
            setAttr("start_time", startDate);
            setAttr("end_time", endDate);
        }
        setAttr(ORG_ID, searchInfo.get(ORG_ID));
        QueryHelper helper = CmccOrderSuspendService.service.getQueryHelper(searchInfo);
        findByPageForSearch(helper.getSelect(), helper.getSqlExceptSelect());
        render("/admin/goventerprise_suspend.tpl");
    }
    
    public void exportAll(){
        ConcurrentHashMap<String, String> searchInfo = getSearchInfo();
        int defaultOrgId = initOrgTree();
        if(searchInfo.get(ORG_ID) == null){
            searchInfo.put(ORG_ID, String.valueOf(defaultOrgId));
        }
        SXSSFWorkbook workbook = CmccOrderSuspendService.service.getExcel(searchInfo);
        render(new ExcelRenderUtil(workbook, "强制开停机"));
   }

}
